#!/usr/bin/perl
#save_interface.cgi

require './network-lib.pl';
&ReadParse();
#&header($text{'edit_interface_title'}, "", "editinterface", 0, 1);
if((defined($in{'nettype'}))&&($in{'nettype'} ne "NB"))
{
	my @slavecard = split(/\s+/, $in{'listinput'});
	if(@slavecard < 2)
	{
		&error($text{'atleast2cards'});
	}
}
#print $in{'nettype'};

my $b = &get_interface($in{'eth'});
$b->{'dhcp'} = 0;
$b->{'bootp'} = 0;
if ($in{'iprdn'} eq 'dhcp'||$in{'iprdn'} eq 'bootp')
{
	&change_gatewaydev($in{'eth'});
	$b->{$in{'iprdn'}} ++;
	$auto = 1;
}
else
{
	&check_ipaddress($in{'ipin'}) || (&nas_syslog("network", "naslog_warning", 16002, "Invalid IP address format"),  &error(&text('invalid_ipaddress', $in{'ipin'})));
	$b->{'address'} = $in{'ipin'};

	&check_ipaddress($in{'maskin'}) || (&nas_syslog("network", "naslog_warning", 16003, "Invalid netmask format"), &error(&text('invalid_netmask', $in{'maskin'})));
	$b->{'netmask'} = $in{'maskin'};

	my @actives = &active_interfaces;
	foreach $a (@actives) {
		next if ($a->{'name'} eq $in{'eth'});
		next if ($a->{'virtual'} eq "SLAVE");
		if($b->{'name'} eq "bond0")
		{
			if($in{'listinput'} =~ /$a->{'name'}/)
			{
				next;
			}
		}
		#&check_ip_conflict($b->{'address'},$b->{'netmask'},$a->{'address'},$a->{'netmask'}) || error($text{'conflict_ip'});
	}

#	&check_ipaddress($in{'broadcastin'}) || (&nas_syslog("network", "naslog_warning", 16004, "Invalid broadcast format"), &error(&text('invalid_broadcast', $in{'broadcastin'})));
	$b->{'broadcast'} = &calc_broadcast($b->{'address'},$b->{'netmask'});

#	&check_ipaddress($in{'gatewayin'}) || (&nas_syslog("network", "naslog_warning", 16005, "Invalid gateway format"), &error(&text('invalid_gateway', $in{'gatewayin'})));
#	$b->{'gateway'} = $in{'gatewayin'};
}
`cp /etc/sysconfig/network-script/ifcfg-bond0 /etc/sysconfig/network-script/ifcfg-bond0.backup`;
&save_interface($b);
=sdfkl
{
	`cp /etc/sysconfig/network-script/ifcfg-bond0.backup /etc/sysconfig/network-script/ifcfg-bond0`;
	&error($text{'config_failed'});
}
=cut
if(defined($in{'nettype'}))
{
	`/etc/sysconfig/network-scripts/bond_cfg stop`;
}
if($in{'nettype'} eq "NLB")
{
	`/etc/sysconfig/network-scripts/bond_cfg start 0`;
}
elsif($in{'nettype'} eq "FT")
{
	`/etc/sysconfig/network-scripts/bond_cfg start 1`;
}
elsif($in{'nettype'} eq "8023AD")
{
	`/etc/sysconfig/network-scripts/bond_cfg start 4`;
}
#if($b->{'name'} ne "bond0")
#{
	&ifdown($b->{'name'});
#}
&ifup($b->{'name'});
`/sbin/ifconfig $b->{'name'} up`;

if($b->{'name'} eq "bond0")
{
	`/etc/sysconfig/network-scripts/bond_cfg xxx`;
}
&nas_syslog("network", "naslog_info", 16001, "Change network interface parameters success!");
if((defined($in{'nettype'}))&&($in{'nettype'} ne "NB"))
{
	my @slavecard = split(/\s+/, $in{'listinput'});
	my $i;
	foreach $i (@slavecard)
	{
#		print "slave $i";
		`/etc/sysconfig/network-scripts/bond_cfg add $i`;	
	}
	#`ifup bond0`;
}
`/etc/init.d/smb restart`;
&redirect("index.cgi");
=xxxx

if($in{'nettype'} eq "NLB")
{
#	print $in{'mac'};
	`/etc/sysconfig/network-scripts/bond_cfg add $in{'mac'}`;
	if($in{'mac'} eq "eth0")
	{
		`/etc/sysconfig/network-scripts/bond_cfg add eth1`;
	}
	elsif($in{'mac'} eq "eth1")
	{
		`/etc/sysconfig/network-scripts/bond_cfg add eth0`;	
	}
#	local $a;
#	local @actives;
#	@actives = &active_interfaces;
#	foreach $a (@actives)
#	{
#		if(($a->{'name'} ne "bond0") && ($a->{'name'} ne $in{'mac'}) && ($a->{'name'} ne "lo")){
#			print $a->{'name'};
#			`/etc/sysconfig/network-scripts/bond_cfg add $a->{'name'}`;
#		}
#	}
}
if($in{'nettype'} eq "FT")
{
#	print $in{'activeadapter'};
	`/etc/sysconfig/network-scripts/bond_cfg add $in{'activeadapter'}`;
	if($in{'activeadapter'} eq "eth0")
	{
		`/etc/sysconfig/network-scripts/bond_cfg add eth1`;
	}
	elsif($in{'activeadapter'} eq "eth1")
	{
		`/etc/sysconfig/network-scripts/bond_cfg add eth0`;
	}
#	local $a;
#	local @actives;
#	@actives = &active_interfaces;
#	foreach $a (@actives)
#	{
#		if(($a->{'name'} ne "bond0") && ($a->{'name'} ne $in{'activeadapter'}) && ($a->{'name'} ne "lo")){
#			print $a->{'name'};
#			`/etc/sysconfig/network-scripts/bond_cfg add $a->{'name'}`;
#		}
#	}
}
=cut

#&footer("index.cgi", $text{'interface_return'});
